package com.example.javaeekeshe.controller;

import com.example.javaeekeshe.pojo.AccountBean;
import com.example.javaeekeshe.service.IAccountService;
import com.example.javaeekeshe.service.IMailService;
import com.example.javaeekeshe.service.IRedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * 登录
 */
@Controller
@RequestMapping("/user")
public class LoginController {
    @Autowired
    IAccountService accountService;
    @Autowired
    IMailService mailService;
    @Autowired
    IRedisService redisService;

    @RequestMapping("/accountLogin")
    public String accountLogin(String userName, String password, Model model, HttpSession session){
        AccountBean accountBean = accountService.existByAccount(userName, password);
        if (accountBean!=null){
            session.setAttribute("loginUser",accountBean);
            return "redirect:/index";
        }else {
            model.addAttribute("msg","用户名或密码错误");
            return "/login";
        }

    }

    @RequestMapping("/existUsername")
    @ResponseBody
    public String existUsername(String email){
//        System.out.println("+==="+email);
        if (accountService.existByUserName(email)!=null){
            return "exist";
        }
        else {
            return "no";
        }

    }

    @RequestMapping("/sendEmail")
    @ResponseBody
    public String sendMail(String email,String is){
        if (accountService.existByUserName(email)!=null&&is==null){
            return "repetition";
        }
        else if (mailService.sendRandomMail(email)){
            return "success";
        }
        else {
            return "sendMailErr";
        }
    }

    /**
     * 注册保存用户
     * @param username
     * @param password
     * @param yzm
     * @param model
     * @return
     */
    @RequestMapping("/saveUser")
    public String saveUser(String username,String password,String yzm,Model model){
        if (accountService.existByUserName(username)!=null){
            model.addAttribute("msg","您已经注册过，请尝试登录或重置密码");
            return "login";
        }
        if (yzm!=null&&yzm.equals(redisService.getValue("yzm"+username))){
            try {
                accountService.saveUser(username,password);
                model.addAttribute("msg","注册成功");
                return "login";
            }
            catch (Exception e){
                model.addAttribute("msg","注册失败");
                return "login";
            }

        }else {
            model.addAttribute("msg","验证码错误");
            return "login";
        }
    }

    @RequestMapping("/update")
    public String update(String userName, String password,String yzm, Model model, HttpSession session){
        if (yzm!=null&&yzm.equals(redisService.getValue("yzm"+userName))){
            try {
                accountService.Update(userName,password);
                model.addAttribute("msg","修改密码成功");
            }catch (Exception e){
                model.addAttribute("msg","修改密码失败");
            }
            return "redirect:/login";
        }else {
            model.addAttribute("msg","验证码错误");
            return "login";
        }
    }

    @RequestMapping("/rename")
    public String rename(String name,String sex,HttpSession session,Model model){
        AccountBean loginUser = (AccountBean) session.getAttribute("loginUser");
        loginUser.setName(name);
        loginUser.setSex(sex);
        try {
            accountService.rename(loginUser);
            session.setAttribute("loginUser",loginUser);
        }catch (Exception e){
            model.addAttribute("msg","修改失败");
        }
        return "redirect:/index";
    }


}
